﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


/******
 * ** 1、F_OUTPUT里的suoding是记录环保人员审核后锁定情况的，当suoding=1时，排放口及排放口相关的申报数据都不能修改及删除了
 * 
 * 
 * 
 * 
 * 
 **/

public partial class _2_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext mydata = new DataClassesDataContext();

        //Session["ShenBaoNianDu"] = "2013";
        //Session["qiyeid"] = 2;
        if (!IsPostBack)
        {
            LabelTiShi.Visible = false;


            // 读取表a01101的经纬度到HiddenFieldJD和HiddenFieldWD
            HiddenFieldJD.Value = "000度00分00秒";
            HiddenFieldWD.Value = "00度00分00秒";
            TextBoxJD.Text = HiddenFieldJD.Value.ToString();
            TextBoxWD.Text = HiddenFieldWD.Value.ToString();
            var query = (from q in mydata.a01101 where q.qiyeid == Convert.ToInt32(Session["qiyeid"]) select q).FirstOrDefault();
            if (query != null)
            {
                if (!(query.XAIS == null || query.XAIS == ""))
                {
                    HiddenFieldJD.Value = query.XAIS.ToString().Substring(0, 3) + "度" + query.XAIS.ToString().Substring(3, 2) + "分" + query.XAIS.ToString().Substring(5, 2) + "秒";
                }
                if (!(query.YAIS == null || query.YAIS == ""))
                {
                    HiddenFieldWD.Value = query.YAIS.ToString().Substring(0, 2) + "度" + query.YAIS.ToString().Substring(2, 2) + "分" + query.YAIS.ToString().Substring(4, 2) + "秒";
                }
            }

            //给排放口下拉框赋初值
            var query2 = (from a in mydata.a01202_1
                          join f in mydata.F_OUTPUT
                              on new { a.qiyeid, a.SERIAL } equals new { f.qiyeid, f.SERIAL }
                          where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"]) && a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                          where f.POLLUTE_TYPE.ToString() == "1" && a.SERIAL < 1000
                          select a).FirstOrDefault();  //可能有几个排放口的数据，默认显示本申报年度有数据的第一个排放口
            if (query2 != null)
            {
                DuShuJu(query2.SERIAL.ToString());
                DropDownListPFK.SelectedValue = query2.SERIAL.ToString();
                HiddenFieldMain.Value = "edit";
            }
            else
            {
                ClearInput();
            }
        }
    }

    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101202");
    }


    //设置排放口的相关信息是否只读。当该排放口是排污征收管理系统里存在的，则不允许修改
    public void SetReadOnly(bool blrd)
    {
        TextBoxPFKMC.Enabled = !blrd;
        TextBoxPFKBH.Enabled = !blrd;
        TextBoxPFKWZ.Enabled = !blrd;
        DropDownListGNQLB.Enabled = !blrd;
        RadioButtonListPFKLX.Enabled = !blrd;
        TextBoxSBMC.Enabled = !blrd;
        DropDownListPFGL.Enabled = !blrd;
        TextBoxPFKGD.Enabled = !blrd;
        TextBoxCKNJ.Enabled = !blrd;
        TextBoxZDJKY.Enabled = !blrd;
    }


    //刷新排放口下拉框显示
    public void ShuaXinXianShi(string SERIAL)
    {

        DropDownListPFK.Items.Clear();
        DropDownListPFK.Items.Add("新增排放口");
        DropDownListPFK.DataBind();
        if (SERIAL != "-1")
        {
            DropDownListPFK.SelectedIndex = DropDownListPFK.Items.IndexOf(DropDownListPFK.Items.FindByValue(SERIAL));
        }
        else//删除排放口后的显示
        {
            DropDownListPFK.SelectedIndex = 0;
        }
    }


    //清空输入框
    public void ClearInput()
    {
        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8 };
        TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8 };
        TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8 };
        TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8 };
        DropDownList[] dpl = new DropDownList[] { this.DropDownList6, this.DropDownList7, this.DropDownList8 };

        HiddenFieldMain.Value = "insert";
        HiddenFieldOUTPUT_EDIT.Value = "insert";
        HiddenFieldA01202_1.Value = "insert";

        SetReadOnly(false);//设置排放口相关信息的输入框为可改写
        TextBoxPFKMC.Text = "";
        TextBoxPFKBH.Text = "";
        TextBoxPFKWZ.Text = "";
        DropDownListGNQLB.SelectedIndex = -1;
        RadioButtonListPFKLX.Items[0].Selected = false;
        RadioButtonListPFKLX.Items[1].Selected = false;
        TextBoxSBMC.Text = "";
        DropDownListPFGL.SelectedIndex = -1;
        TextBoxPFTS.Text = "";
        TextBoxPFSJ.Text = "";
        TextBoxPFKGD.Text = "";
        TextBoxCKNJ.Text = "";
        TextBoxZJRL.Text = "";
        TextBoxFQPFL.Text = "";
        TextBoxQZDBPFL.Text = "";
        TextBoxQZCBPFL.Text = "";
        for (int i = 0; i < 12; i++)
        {
            CheckBoxList1.Items[i].Selected = false;
        }
        TextBoxZDJKY.Text = "";
        for (int i = 0; i < 8; i++)
        {
            hb[i].Value = "insert";
            tbxHJ[i].Text = "";
            tbxDBL[i].Text = "";
            tbxCBL[i].Text = "";
            if (i >= 5)
            {
                dpl[i - 5].SelectedIndex = -1;
            }
        }
    }


    //读一个排放口的资料数据和对应的本年度的申报数据,调用前要判断SERIAL不能为空，确保是有数据的排口
    public void DuShuJu(string SERIAL)
    {


        ClearInput();

        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {
            //读排放口信息
            var query = (from f in mydata.F_OUTPUT
                         where f.qiyeid == Convert.ToInt32(Session["qiyeid"]) && f.SERIAL == Convert.ToInt32(SERIAL)
                         select new
                         {
                             pfkmc = f.OUT_NAME,
                             pfkbh = f.OUTPUT_CODE,
                             pfkwz = f.OUTPUT_POSITION,
                             gnqlb = f.FUNCTION_REGION == null ? -1 : Convert.ToInt32(f.FUNCTION_REGION),
                             //sflkq=f.IF_TWOAREA==null?false:Convert.ToBoolean(f.IF_TWOAREA),
                             pfklx = f.OUT_TYPE == null ? 0 : Convert.ToInt32(f.OUT_TYPE),
                             sbmc = f.MACHINE_NAME == null ? "" : f.MACHINE_NAME,
                             pfgl = f.OUT_RULE == null ? "" : Convert.ToString(f.OUT_RULE),
                             pfkgd = f.GAS_HIGHT == null ? "" : string.Format("{0:#.##}", f.GAS_HIGHT),
                             cknj = f.RADIUS == null ? "" : string.Format("{0:#.##}", f.RADIUS),
                             zdjky = f.FACILITY_NAME == null ? "" : f.FACILITY_NAME,
                             suoding = f.suoding == null ? false : Convert.ToBoolean(f.suoding),
                             system = f.system == null ? false : Convert.ToBoolean(f.system)
                         }).FirstOrDefault();
            if (query != null)
            {
                //*************排放口信息*********
                TextBoxPFKMC.Text = query.pfkmc;
                TextBoxPFKBH.Text = query.pfkbh;
                TextBoxPFKWZ.Text = query.pfkwz;
                TextBoxJD.Text = Convert.ToString(HiddenFieldJD.Value);
                TextBoxWD.Text = Convert.ToString(HiddenFieldWD.Value);
                DropDownListGNQLB.SelectedIndex = query.gnqlb;
                if (query.pfklx == 1)
                {
                    RadioButtonListPFKLX.Items[0].Selected = true;
                }
                else if (query.pfklx == 2)
                {
                    RadioButtonListPFKLX.Items[1].Selected = true;
                }
                TextBoxSBMC.Text = query.sbmc;
                if (query.pfgl.Trim() == "")
                {
                    DropDownListPFGL.SelectedIndex = -1;
                }
                else
                {
                    DropDownListPFGL.SelectedValue = query.pfgl;
                }
                TextBoxPFKGD.Text = query.pfkgd;
                TextBoxCKNJ.Text = query.cknj;
                TextBoxZDJKY.Text = query.zdjky;

                //判断排放口信息是否可以修改
                SetReadOnly(query.suoding || query.system);//如果该排放口信息已被审核后锁定或是排污征收系统数据库导出的数据则不能修改
                if (query.suoding || query.system) //置排放口属性信息不可修改标志
                {
                    HiddenFieldOUTPUT_EDIT.Value = "insert";
                }
                else
                {
                    HiddenFieldOUTPUT_EDIT.Value = "edit";
                }

                //************排放口除了污染物外申报数据*********
                var q = (from a in mydata.a01202_1
                         where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                         where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                         where a.SERIAL == Convert.ToInt32(SERIAL)
                         select new
                         {
                             pfts = a.OUT_DAYS == null ? "" : string.Format("{0:0}", a.OUT_DAYS),
                             pfsj = a.HOURS_DAY == null ? "" : string.Format("{0:0}", a.HOURS_DAY),
                             zjrl = a.FACILITY_W == null ? "" : string.Format("{0:#.##}", a.FACILITY_W),
                             fqpfl = a.SUM_COUNT == null ? "" : string.Format("{0:#.####}", a.SUM_COUNT),
                             qzdbpfl = a.REACH_CNT == null ? "" : string.Format("{0:#.####}", a.REACH_CNT),
                             qzcbpfl = a.OVER_CNT == null ? "" : string.Format("{0:#.####}", a.OVER_CNT),
                             pfyf = a.OUT_MONTH == null ? "" : Convert.ToString(a.OUT_MONTH)
                         }).FirstOrDefault();
                if (q != null)
                {
                    HiddenFieldA01202_1.Value = "edit"; //置该排放口申报数据可修改标记

                    TextBoxPFTS.Text = q.pfts;
                    TextBoxPFSJ.Text = q.pfsj;
                    TextBoxZJRL.Text = q.zjrl;
                    TextBoxFQPFL.Text = q.fqpfl;
                    TextBoxQZDBPFL.Text = q.qzdbpfl;
                    TextBoxQZCBPFL.Text = q.qzcbpfl;

                    //排放月份
                    for (int i = 0; i < 12; i++)
                    {
                        CheckBoxList1.Items[i].Selected = false;
                    }
                    string[] fh = new string[] { "," };
                    string[] yf;
                    string s = q.pfyf;
                    if (q.pfyf != "")
                    {
                        yf = s.Split(fh, StringSplitOptions.None);

                        foreach (var cs in yf)
                        {
                            CheckBoxList1.Items[Convert.ToInt32(cs) - 1].Selected = true;
                        }
                    }
                }


                //**********读取污染物数据*********** 
                HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8 };
                TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8 };
                TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8 };
                TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8 };
                DropDownList[] dpl = new DropDownList[] { this.DropDownList6, this.DropDownList7, this.DropDownList8 };

                for (int i = 0; i < 8; i++)//转换另外的排放口的初始化数据
                {
                    hb[i].Value = "insert";
                }

                var record = from a in mydata.a01202_2
                             where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                             where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                             where a.SERIAL == Convert.ToInt32(SERIAL)
                             select a;
                if (record != null)
                {
                    int xh;
                    foreach (var r in record)
                    {
                        xh = Convert.ToInt32(r.SRKXH) - 1;
                        hb[xh].Value = "edit";
                        if (xh >= 5)
                        {
                            dpl[xh - 5].SelectedValue = r.CODE == null ? "" : r.CODE;
                        }
                        tbxHJ[xh].Text = r.POLLUTE_OUTCOUNT == null ? "" : string.Format("{0:#.####}", r.POLLUTE_OUTCOUNT);
                        tbxDBL[xh].Text = r.REACH_COUNT == null ? "" : string.Format("{0:#.####}", r.REACH_COUNT);
                        tbxCBL[xh].Text = r.OVER_COUNT == null ? "" : string.Format("{0:#.####}", r.OVER_COUNT);
                    }
                }
            }
        }
    }


    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8 };

        if (Page.IsValid) //当所有校验控件都正确是才提交
        {
            DataClassesDataContext mydata = new DataClassesDataContext();

            //得到排放口的SERIAL
            string SERIAL = "0";
            if (DropDownListPFK.SelectedIndex != 0)//第一个item要永远保证是“新增排放口”
            {
                SERIAL = Convert.ToString(DropDownListPFK.SelectedValue);//排放口变量赋值 
            }
            else //新增排放口要生成新的SERIAL
            {
                int? max = (from f in mydata.F_OUTPUT
                            where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                            where f.SERIAL < 1000
                            select f.SERIAL).Max();
                if (!max.HasValue)
                    SERIAL = "1";
                else
                    SERIAL = Convert.ToString(max + 1);
            }

            //**********逻辑判断***********
            //应加入判断 1、任何时候，排放口名称和排放口编号不能为空，同时不能跟已有的排放口名称、编号重名;
            //                      [6.功能区类别]、[9.排放口类型]、[11.废气排放规律]不能为空;
            //                       行业类别由基本信息表导入
            //          2、

            string strCuoWu = "提交内容有错误：";//记录错误内容
            int CuoWuXuHao = 0;//错误序号
            bool TuiChu = false; //记录逻辑判断过程中是否有错误需要中断退出提交过程
            if (TextBoxPFKMC.Text.Trim() == "")
            {
                CuoWuXuHao++;
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "排放口名称不能为空。";
                TuiChu = true;
            }
            else
            {
                bool cz1 = (from f in mydata.F_OUTPUT
                            where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                            where f.SERIAL != Convert.ToInt32(SERIAL)
                            where f.OUT_NAME == TextBoxPFKMC.Text.Trim()
                            select f).Any();
                if (cz1 == true)
                {
                    CuoWuXuHao++;
                    strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "已有排放口'" + TextBoxPFKMC.Text.Trim() + "',不能重复。<br/>";
                    TuiChu = true;
                }
            }

            if (TextBoxPFKBH.Text.Trim() == "")
            {
                CuoWuXuHao++;
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "排放口编号不能为空。";
                TuiChu = true;
            }
            else
            {
                bool cz2 = (from f in mydata.F_OUTPUT
                            where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                            where f.SERIAL != Convert.ToInt32(SERIAL)
                            where f.OUTPUT_CODE == TextBoxPFKBH.Text.Trim()
                            select f).Any();
                if (cz2 == true)
                {
                    CuoWuXuHao++;
                    strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "已有排放口编号'" + TextBoxPFKBH.Text.Trim() + "',不能重复。<br/>";
                    TuiChu = true;
                }
            }

            if (RadioButtonListPFKLX.Items[0].Selected || RadioButtonListPFKLX.Items[1].Selected)
            {
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + " 排放口类型必须选择一个";
            }

            if (DropDownListGNQLB.Text.Trim() == "")
            {
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + " 功能区类别不能为空";
            }

            if (DropDownListPFGL.Text.Trim() == "")
            {
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + " 废气排放规律不能为空";
            }

            if (TuiChu)
            {
                Response.Write("<script>alert('提交内容有错误，请检查!')</script>");
                TextBoxCuoWuXinXi.Text = strCuoWu;
                return;//中断退出提交过程
            }

            LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
            LabelTiShi.Visible = true;
            TextBoxCuoWuXinXi.Text = "";


            //**************新增或修改排放口信息
            if (HiddenFieldMain.Value == "edit")//在DropDownListPFK里选择的不是“新增排放口”
            {
                if (HiddenFieldOUTPUT_EDIT.Value == "edit")//排放口不是是排污系统数据库导出或不是已通过审核锁定的，可修改
                {
                    string jd = Convert.ToString(HiddenFieldJD.Value);
                    string wd = Convert.ToString(HiddenFieldWD.Value);

                    var rd = (from f in mydata.F_OUTPUT
                              where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                              where f.SERIAL == Convert.ToInt32(SERIAL)
                              select f).FirstOrDefault();
                    rd.OUT_NAME = TextBoxPFKMC.Text.Trim();
                    rd.OUTPUT_CODE = TextBoxPFKBH.Text.Trim();
                    rd.OUTPUT_POSITION = TextBoxPFKWZ.Text.Trim();
                    rd.XAIS = Convert.ToDouble(jd.Substring(0, 3) + jd.Substring(4, 2) + jd.Substring(7, 2));
                    rd.YAIS = Convert.ToDouble(wd.Substring(0, 2) + wd.Substring(3, 2) + wd.Substring(6, 2));
                    rd.FUNCTION_REGION = Convert.ToInt32(DropDownListGNQLB.SelectedValue);
                    rd.OUT_TYPE = RadioButtonListPFKLX.SelectedIndex + 1;
                    rd.OUT_RULE = Convert.ToString(DropDownListPFGL.SelectedValue);
                    rd.GAS_HIGHT = Convert.ToDouble(TextBoxPFKGD.Text.Trim());
                    rd.RADIUS = Convert.ToDouble(TextBoxCKNJ.Text.Trim());
                    rd.FACILITY_NAME = TextBoxZDJKY.Text.Trim();
                    mydata.SubmitChanges();
                }
            }
            else//新增排放口
            {
                F_OUTPUT p = new F_OUTPUT();
                p.OUT_NAME = TextBoxPFKMC.Text.Trim();
                p.OUTPUT_CODE = TextBoxPFKBH.Text.Trim();
                p.OUTPUT_POSITION = TextBoxPFKWZ.Text.Trim();
                p.XAIS = Convert.ToDouble(HiddenFieldJD.Value.ToString().Substring(0, 3) + HiddenFieldJD.Value.ToString().Substring(4, 2) + HiddenFieldJD.Value.ToString().Substring(7, 2));
                p.YAIS = Convert.ToDouble(HiddenFieldWD.Value.ToString().Substring(0, 2) + HiddenFieldWD.Value.ToString().Substring(3, 2) + HiddenFieldWD.Value.ToString().Substring(6, 2));
                p.FUNCTION_REGION = Convert.ToInt32(DropDownListGNQLB.SelectedValue);
                p.OUT_TYPE = RadioButtonListPFKLX.SelectedIndex + 1;
                p.OUT_RULE = Convert.ToString(DropDownListPFGL.SelectedValue);
                p.GAS_HIGHT = Convert.ToDouble(TextBoxPFKGD.Text.Trim());
                p.RADIUS = Convert.ToDouble(TextBoxCKNJ.Text.Trim());
                p.FACILITY_NAME = TextBoxZDJKY.Text.Trim();
                p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                p.SERIAL = Convert.ToInt32(SERIAL);
                p.POLLUTE_TYPE = Convert.ToChar("2");
                p.LOGOUT_FLAG = 0;
                mydata.F_OUTPUT.InsertOnSubmit(p);
                mydata.SubmitChanges();

                HiddenFieldOUTPUT_EDIT.Value = "edit";
                HiddenFieldMain.Value = "edit";
                HiddenFieldA01202_1.Value = "insert";

                for (int i = 0; i < 8; i++)//转换另外的排放口的初始化数据
                {
                    hb[i].Value = "insert";
                }

                ShuaXinXianShi(SERIAL);
            }


            //*******************保存排放口除了污染物外本年度申报数据
            ZhuanHuan zh = new ZhuanHuan();//调用转换类
            string sss = "";
            for (int i = 0; i < 12; i++)//排放月份
            {
                if (CheckBoxList1.Items[i].Selected == true)
                {
                    sss = sss + Convert.ToString(i + 1) + ",";
                }
            }
            if (sss != "")
            {
                sss = sss.Substring(0, sss.Length - 1);//去掉最后的","  
            }

            if (HiddenFieldA01202_1.Value == "edit")
            {
                var rd3 = (from a in mydata.a01202_1
                           where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                           where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                           where a.SERIAL == Convert.ToInt32(SERIAL)
                           select a).First();
                rd3.OUT_DAYS = zh.StrToDouble(TextBoxPFTS.Text.Trim());
                rd3.HOURS_DAY = zh.StrToDouble(TextBoxPFSJ.Text.Trim());
                rd3.FACILITY_W = zh.StrToDouble(TextBoxZJRL.Text.Trim());
                rd3.SUM_COUNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim()) + zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                rd3.REACH_CNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim());
                rd3.OVER_CNT = zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                rd3.OUT_MONTH = sss;
                mydata.SubmitChanges();
            }
            else
            {
                a01202_1 p = new a01202_1();
                p.OUT_DAYS = zh.StrToDouble(TextBoxPFTS.Text.Trim());
                p.HOURS_DAY = zh.StrToDouble(TextBoxPFSJ.Text.Trim());
                p.FACILITY_W = zh.StrToDouble(TextBoxZJRL.Text.Trim());
                p.SUM_COUNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim()) + zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                p.REACH_CNT = zh.StrToDouble(TextBoxQZDBPFL.Text.Trim());
                p.OVER_CNT = zh.StrToDouble(TextBoxQZCBPFL.Text.Trim());
                p.OUT_MONTH = sss;
                p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                p.SERIAL = Convert.ToInt32(SERIAL);
                mydata.a01202_1.InsertOnSubmit(p);
                mydata.SubmitChanges();

                HiddenFieldA01202_1.Value = "edit";

            }

            //************update 污染物********
            //********申报表里污染物（1）二氧化硫 到 （5）挥发性有机溶剂*********
            //
            //污染物代码，按申报表里污染物名称前面的序号排列,记录(1)汞到(5)悬浮物
            // " 02"二氧化硫, " 01"烟尘, " 50"工业粉尘, " 03"氮氧化物, " 60"挥发性有机溶剂
            string[] wrwdm = new string[] { " 02", " 01", " 50", " 03", " 60" };

            //HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
            TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8 };
            TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8 };
            TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8 };

            for (int i = 0; i <= 4; i++)
            {
                if (hb[i].Value == "edit")
                {
                    var rd4 = (from a in mydata.a01202_2
                               where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                               where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                               where a.SERIAL == Convert.ToInt32(SERIAL)
                               where a.SRKXH == Convert.ToString(i + 1)
                               select a).First();
                    rd4.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                    rd4.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                    rd4.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                    mydata.SubmitChanges();

                    tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                }
                else//新增
                {
                    if ((tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                    {
                        a01202_2 p = new a01202_2();

                        p.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                        p.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                        p.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                        p.SRKXH = Convert.ToString(i + 1);
                        p.CODE = wrwdm[i];
                        p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                        p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                        p.SERIAL = Convert.ToInt32(SERIAL);
                        mydata.a01202_2.InsertOnSubmit(p);
                        mydata.SubmitChanges();

                        hb[i].Value = "edit";
                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                }
            }

            //*******污染物(6) 到 (18)*********用户自己选择污染物呢名称的
            DropDownList[] dpl = new DropDownList[] { this.DropDownList6, this.DropDownList7, this.DropDownList8 };
            for (int i = 5; i <= 7; i++)
            {
                if (hb[i].Value == "edit")
                {
                    var rd5 = (from a in mydata.a01202_2
                               where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                               where a.SRKXH == Convert.ToString(i + 1)
                               where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                               where a.SERIAL == Convert.ToInt32(SERIAL)
                               select a).First();

                    rd5.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                    rd5.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                    rd5.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                    rd5.CODE = dpl[i - 11].SelectedValue;
                    mydata.SubmitChanges();

                    tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                }
                else//新增
                {
                    if ((tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                    {
                        a01202_2 p = new a01202_2();

                        p.POLLUTE_OUTCOUNT = zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim());
                        p.REACH_COUNT = zh.StrToDouble(tbxDBL[i].Text.Trim());
                        p.OVER_COUNT = zh.StrToDouble(tbxCBL[i].Text.Trim());
                        p.SRKXH = Convert.ToString(i + 1);
                        p.CODE = dpl[i - 5].SelectedValue;
                        p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                        p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                        p.SERIAL = Convert.ToInt32(SERIAL);
                        mydata.a01202_2.InsertOnSubmit(p);
                        mydata.SubmitChanges();

                        hb[i].Value = "edit";
                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                }
            }

            LabelTiShi.Visible = false;
            //Response.Write("<script>alert('你所填写申报内容正确，已成功提交!')</script>");
            MessageBox("tishi", "你所填写申报内容正确，已成功提交!");
        }
    }


    //// <summary> 
    /// 弹出提示信息框 
    /// </summary> 
    /// <param name="strKey">函数key值</param> 
    /// <param name="strInfo">提示信息</param> 
    public void MessageBox(string strKey, string strInfo)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(strKey))
        {
            string strjs = "alert('" + strInfo + "');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
        }
    }

    protected void DropDownListPFK_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownListPFK.SelectedIndex > 0)
        {
            DuShuJu(DropDownListPFK.SelectedValue);
            HiddenFieldMain.Value = "edit";

            Session["PaiFangKou2-2"] = DropDownListPFK.SelectedValue;//传参数给打印页面用
        }
        else//新增排放口
        {
            ClearInput();
            Session["PaiFangKou2-2"] = "";//传参数给打印页面用
        }

    }
    protected void LinkButtonDelPFK_Click(object sender, EventArgs e)
    {
        if (DropDownListPFK.SelectedIndex == 0)
        {
            Response.Write("<script>alert('没有选择到要删除的排放口!')</script>");
            return;
        }

        DataClassesDataContext mydata = new DataClassesDataContext();

        //在F_OUTPUT判断是否属于不能删除的排放口信息，
        // 如果suoding=1，则排放口及对应的本年度、以前所有年度申报数据都不能删除
        bool cz1 = (from f in mydata.F_OUTPUT
                    where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                    where f.suoding == true
                    where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                    select f).Any();
        if (cz1)
        {
            Response.Write("<script>alert('该排放口的申报信息已通过审核被锁定了，不能删除!')</script>");
            return;
        }

        //删除本年度已填报的申报数据,包括表a01202_1和表a01202_2。往年申报数据不能删除
        var rd1 = (from a in mydata.a01202_1
                   where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                   where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                   where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                   select a).FirstOrDefault();
        if (rd1 != null)
        {
            mydata.a01202_1.DeleteOnSubmit(rd1);
            mydata.SubmitChanges();
        }

        var rd2 = (from a in mydata.a01202_2
                   where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                   where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                   where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                   select a).FirstOrDefault();
        if (rd2 != null)
        {
            mydata.a01202_2.DeleteOnSubmit(rd2);
            mydata.SubmitChanges();
        }

        //只有当F_OUTPUT里的字段system=0和该排放口在本年度之前的年度没有申报数据两个条件同时成立，才能删除该排放口
        var cz2 = (from f in mydata.F_OUTPUT
                   where (f.system == null || f.system == false)
                   where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                   where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                   select f).Any();
        if (cz2)
        {
            bool cz3 = (from a in mydata.a01202_1
                        where a.DATA_MONTH.CompareTo(Convert.ToString(Session["ShenBaoNianDu"])) == -1
                        where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                        where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                        select a).Any();
            if (!cz3)
            {
                bool cz4 = (from a in mydata.a01202_2
                            where a.DATA_MONTH.CompareTo(Convert.ToString(Session["ShenBaoNianDu"])) == -1
                            where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                            where a.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                            select a).Any();
                if (!cz3)
                {
                    var rd3 = (from f in mydata.F_OUTPUT
                               where f.qiyeid == Convert.ToInt32(Session["qiyeid"])
                               where f.SERIAL == Convert.ToInt32(DropDownListPFK.SelectedValue)
                               select f).FirstOrDefault();
                    mydata.F_OUTPUT.DeleteOnSubmit(rd3);
                    mydata.SubmitChanges();

                    ClearInput();
                    ShuaXinXianShi("0");
                    Response.Write("<script>alert('删除排放口成功')</script>");
                }
            }
        }
        else
        {
            Response.Write("<script>alert('该排放口被系统管理员锁定，不能删除!只能删除本年度的申报信息。')</script>");
            return;
        }
    }

}